package com.flyme.module.base.provider.controller;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.StringUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysRoleRemoteService;
import com.flyme.module.base.client.constants.BaseConstants;
import com.flyme.module.base.client.entity.SysAuthority;
import com.flyme.module.base.client.entity.SystemRole;
import com.flyme.module.base.provider.service.SysAuthorizeService;
import com.flyme.module.base.provider.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zyf
 */
@Api(tags = "角色管理")
@RestController
public class SysRoleController extends BaseController<SysRoleService, SystemRole> implements SysRoleRemoteService {
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysAuthorizeService grantAccessService;
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 角色列表
     *
     * @return
     */
    @ApiOperation(value = "获取角色分页列表", notes = "获取角色分页列表")
    @PostMapping("/role/list")
    @Override
    public ResultBody list(@ModelAttribute(value = "pagerInfo") PagerInfo pagerInfo) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("id,roleName,roleCode,roleDesc,defaultRole,status,createTime,updateTime");
        cq.orderByAsc("sortOrder");
        PqGrid pqGrid = bizService.pageList(cq);
        // 推送
        messagingTemplate.convertAndSend("/topic/subscribe", "您收到了新的系统消息");
        return ResultBody.success(pqGrid);
    }

    /**
     * 获取角色信息
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "获取角色信息", notes = "获取角色信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "path")})
    @GetMapping("/role/{roleId}")
    @Override
    public ResultBody getRole(@PathVariable(value = "roleId") Long roleId) {
        SystemRole result = roleService.getById(roleId);
        return ResultBody.success(result);
    }

    /**
     * 添加角色
     *
     * @param role 角色
     * @return
     */
    @ApiOperation(value = "添加角色", notes = "添加角色")
    @PostMapping("/role/add")
    @Override
    public ResultBody addRole(@ModelAttribute(value = "role") SystemRole role) {
        role.setStatus(BaseConstants.ENABLED);
        boolean result = roleService.addRole(role);
        return ResultBody.success(result);
    }

    /**
     * 更新角色
     *
     * @param role   角色
     * @return
     */
    @ApiOperation(value = "编辑角色", notes = "编辑角色")
    @PostMapping("/role/edit")
    @Override
    public ResultBody edit(@ModelAttribute(value = "role") SystemRole role) {
        boolean result = roleService.updateRole(role);
        return ResultBody.success(result);
    }


    /**
     * 更新状态
     *
     * @param roleId 角色ID
     * @return
     */
    @ApiOperation(value = "更新角色状态", notes = "更新角色状态")
    @PostMapping("/role/update/status")
    @Override
    public ResultBody updateStatus(@RequestParam("roleId") Long roleId, @RequestParam(value = "status", defaultValue = "1") Integer status) {
        boolean result = roleService.updateStatus(roleId, status);
        return ResultBody.success(result);
    }


    /**
     * 设置注册默认角色
     *
     * @param id 角色ID
     * @return
     */
    @ApiOperation(value = "更新角色状态", notes = "更新角色状态")
    @PostMapping("/role/setDefault")
    @Override
    public ResultBody setDefault(@RequestParam("id") Long id, @RequestParam(value = "defaultRole", defaultValue = "1") Integer defaultRole) {
        boolean result = roleService.setDefault(id, defaultRole);
        return ResultBody.success(result);
    }

    /**
     * 删除角色
     *
     * @param roleIds 角色ID
     * @return
     */
    @ApiOperation(value = "删除角色", notes = "删除角色")
    @PostMapping("/role/delByIds")
    @Override
    public ResultBody delByIds(@RequestParam String[] roleIds) {
        boolean result = roleService.delByRoleIds(roleIds);
        return ResultBody.success(result);
    }

    /**
     * 菜单授权
     *
     * @param roleId  角色ID
     * @param menuIds 菜单ID.多个以,隔开
     * @return
     */
    @ApiOperation(value = "菜单授权", notes = "菜单授权")
    @PostMapping("/role/grant/menu")
    @Override
    public ResultBody<Boolean> roleGrantMenu(@RequestParam(value = "roleId") Long roleId, @RequestParam("menuIds") String[] menuIds) {
        String result = grantAccessService.addGrantAccess(String.valueOf(roleId), BaseConstants.AUTHORITY_PREFIX_ROLE, BaseConstants.RESOURCE_TYPE_MENU, menuIds);
        return ResultBody.success(StringUtils.isNotBlank(result));
    }

    /**
     * 操作授权
     *
     * @param roleId    角色ID
     * @param actionIds 操作ID.多个以,隔开
     * @return
     */
    @ApiOperation(value = "操作授权", notes = "操作授权")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", defaultValue = "", required = true, paramType = "form"), @ApiImplicitParam(name = "actionIds", value = "操作ID.多个以,隔开", defaultValue = "", required = true, paramType = "form")})
    @PostMapping("/role/grant/action")
    @Override
    public ResultBody<Boolean> roleGrantAction(@RequestParam(value = "roleId") Long roleId, @RequestParam("actionIds") String actionIds) {
        String result = grantAccessService.addGrantAccess(String.valueOf(roleId), BaseConstants.AUTHORITY_PREFIX_ROLE, BaseConstants.RESOURCE_TYPE_ACTION, actionIds.split(","));
        return ResultBody.success(StringUtils.isNotBlank(result));
    }

    /**
     * 接口授权
     *
     * @param roleId 角色ID
     * @param apiIds 接口ID.多个以,隔开
     * @return
     */
    @ApiOperation(value = "接口授权", notes = "接口授权")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", defaultValue = "", required = true, paramType = "form"), @ApiImplicitParam(name = "apiIds", value = "接口ID.多个以,隔开", defaultValue = "", required = true, paramType = "form")})
    @PostMapping("/role/grant/api")
    @Override
    public ResultBody<Boolean> roleGrantApi(@RequestParam(value = "roleId") Long roleId, @RequestParam("apiIds") String apiIds) {
        String result = grantAccessService.addGrantAccess(String.valueOf(roleId), BaseConstants.AUTHORITY_PREFIX_ROLE, BaseConstants.RESOURCE_TYPE_API, apiIds.split(","));
        return ResultBody.success(StringUtils.isNotBlank(result));
    }

    /**
     * 获取角色已授权菜单资源
     *
     * @param roleId 角色ID
     * @return
     */
    @ApiOperation(value = "获取角色已授权菜单资源", notes = "获取角色已授权菜单资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "form")})
    @PostMapping("/role/granted/menu")
    @Override
    public ResultBody<List<SysAuthority>> roleGrantedMenu(Long roleId) {
        List<SysAuthority> result = grantAccessService.findList(String.valueOf(roleId), BaseConstants.AUTHORITY_PREFIX_ROLE, BaseConstants.RESOURCE_TYPE_MENU);
        return ResultBody.success(result);
    }

    /**
     * 获取角色已授权操作资源
     *
     * @param roleId 角色ID
     * @return
     */
    @ApiOperation(value = "获取角色已授权操作资源", notes = "获取角色已授权操作资源")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色ID", required = true, paramType = "form")})
    @PostMapping("/role/granted/action")
    @Override
    public ResultBody<List<SysAuthority>> roleGrantedAction(Long roleId) {
        List<SysAuthority> result = grantAccessService.findList(String.valueOf(roleId), BaseConstants.AUTHORITY_PREFIX_ROLE, BaseConstants.RESOURCE_TYPE_ACTION);
        return ResultBody.success(result);
    }

    /**
     * 获取角色列表
     *
     * @return
     */
    @ApiOperation(value = "获取角色列表", notes = "获取角色列表")
    @GetMapping("/role/selertRoleList")
    @Override
    public ResultBody selertRoleList() {
        List<SystemRole> list = bizService.selertRoleList();
        return ResultBody.success(list);
    }
}
